﻿using Common.Exceptions;
using Microsoft.AspNetCore.Mvc;
using OpenIddict.Client;
using ProductService.Services;
using ProductService.Services.Dtos;
using SeckillService.Services;
using SeckillService.Services.Dtos;
using Servicecomb.Saga.Omega.Abstractions.Transaction;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;
using WebSite_Aggregate.Services.Dtos;

namespace ProductService.Controllers;

/// <summary>
///  秒杀首页控制器(秒杀首页接口)
/// </summary>
[Route("ebusinessseckills/seckill")]
public class SeckillController : AbpController
{
    public ISeckillService seckillService { get; set; }

    public SeckillController()
    {

    }

    /// <summary>
    /// 查询时间秒杀
    /// </summary>
    /// <param name="getSeckillTimeDto"></param>
    /// <returns></returns>
    [HttpGet("SeckillTime")]
    public async Task<List<SeckillDto>> GetSeckillTime([FromQuery] GetSeckillTimeDto getSeckillTimeDto)
    {
        var dto = await seckillService.GetListAsync(new PagedAndSortedResultRequestDto());
        DateTime currentTime = DateTime.Now;
        DateTime currentDate = currentTime.Date;
        //过滤秒杀时间商品
        List<SeckillDto> seckillDtos = dto.Items.Where(x =>
                     DateTime.Compare(currentDate, DateTime.Parse(x.SeckillDate)) <= 0 &&
                     DateTime.Compare(currentTime, DateTime.Parse(x.SeckillDate + " " + x.SeckillEndtime)) < 0).ToList();
        return seckillDtos;
    }

    /// <summary>
    /// 查询秒杀
    /// </summary>
    /// <param name="SeckillId"></param>
    /// <returns></returns>
    [HttpGet("SeckillProducts")]
    public Task<List<SeckillProductDto>> GetSeckillProductsAsync(Guid SeckillId)
    {
        return seckillService.GetSeckillProductsAsync(SeckillId);
    }
}
